'''
$LastChangedBy: csgillespie $
$LastChangedDate: 2007-06-06 11:08:06 +0000 (Wed, 06 Jun 2007) $
Some basic unittesting
Really need to expand this.
'''
import unittest
import os, sys
DIR = '/data/ncsg3/svn/pythonR/BasisTools/trunk/'
sys.path.insert(0, DIR )
from pysbml import pysbml

class TestModelBuilding(unittest.TestCase):
	'''Tests the model building'''
	
	def testBuilding1(self):
		'''Model building 1'''
		e = pysbml(path = DIR).sbmlModel()
		e.setId('LV_Model')
		e.setNotes('<body xmlns="http://www.w3.org/1999/xhtml">\
			This is the tradional LV model</body>')

		e.addCompartments(Id='Cell')
		e.addSpecies(Id='ATP', Compartment='Cell', InitialAmount=0)
		e.addSpecies(Id='ADP', Compartment='Cell', InitialAmount=0)
		e.addReactions(Id='Conversion', listOfReactants=[{'Species':'ATP'}], \
			listOfProducts=[{'Species':'ADP'}], KineticLaw={'Formula':'a * ATP'})
		e.addParameters(Id='a', Value=1)
		e.validate()
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 1)
		self.assertEqual(e.m.getNumParameters(), 1)
	
	def testBuilding2(self):
		'''Model building 2'''
		e = pysbml(path = DIR).sbmlModel()
		e.setId('LV_Model')
		e.setNotes('<body xmlns="http://www.w3.org/1999/xhtml">\
				This is the tradional LV model</body>')

		e.add(Compartments = {'Id':'Cell'})
		e.add(Species = {'Id':'X', 'InitialAmount':10, 'Compartment':'Cell'})
		e.add(Species = {'Id':'Y', 'InitialAmount':10, 'Compartment':'Cell'})

		e.add(Parameters = {'Id':'a', 'Value':1})
		e.add(Parameters = {'Id':'b', 'Value':1})
		e.add(Parameters = {'Id':'c', 'Value':1})

		e.add(Reactions = {'Id':'PreyReproduction', 
			'listOfReactants':[{'Species':'X'}],
			'listOfProducts':[{'Species':'X','Stoichiometry':2}], 
			'KineticLaw':{'Formula':'X'}})
		
		e.add(Reactions = {'Id':'PredatorReproduction', 
			'listOfReactants':[{'Species':'X'},{'Species':'Y'}],
			'listOfProducts':[{'Species':'Y','Stoichiometry':2}], 
			'KineticLaw':{'Formula':'b * X * Y'}})

		e.add(Reactions = {'Id':'PredatorDecay', 'Reversible':0,
			'listOfReactants':[{'Species':'Y'}],
			'listOfProducts':[{'Species':'Y','Stoichiometry':1}],
			'KineticLaw':{'Formula':'c*Y'}})	
		
		e.validate()
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 3)
		self.assertEqual(e.m.getNumParameters(), 3)

		
class TestModelDeleting(unittest.TestCase):
	
	def testDeleteCompartments(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		e.deleteCompartments('compartment')
		self.assertEqual(e.m.getNumCompartments(), 0)
		self.assertEqual(e.m.getNumSpecies(), 0)
		self.assertEqual(e.m.getNumReactions(), 0)
		self.assertEqual(e.m.getNumParameters(), 3)
	
	def testDeleteSpecies(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		e.deleteSpecies('X')
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 1)
		self.assertEqual(e.m.getNumReactions(), 1)
		self.assertEqual(e.m.getNumParameters(), 3)
	
	def testDeleteParameters(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		e.deleteParameters('a')
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 2)
		self.assertEqual(e.m.getNumParameters(), 2)
	
	def testDeleteReactions(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		e.deleteReactions('PreyProduction')
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 2)
		self.assertEqual(e.m.getNumParameters(), 3)

	
class TestModelModify(unittest.TestCase):
	
	def testModifyCompartments(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		
		e.modifyCompartments(compartment={'Id':'new_cp'})
		new_cp_id = e.m.getListOfCompartments()[0].getId()
		
		#Check cp id has been changed
		self.assertEqual(new_cp_id, 'new_cp')
		
		#Check sp cp has been changed
		sp_cp = e.m.getListOfSpecies()[0].getCompartment()
		self.assertEqual(sp_cp , 'new_cp')
		
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 3)
		self.assertEqual(e.m.getNumParameters(), 3)	
	
	def testModifySpecies(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		
		e.modifySpecies(X={'Id':'new_X', 'InitialAmount':0})
		sp = e.m.getListOfSpecies()[0]
		self.assertEqual(sp.getId(), 'new_X')
		self.assertEqual(int(sp.getInitialAmount()), 0)
		
		new_kl = e.m.getListOfReactions()[0].getKineticLaw().getFormula()
		self.assertEqual(new_kl, 'a * new_X')
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 3)
		self.assertEqual(e.m.getNumParameters(), 3)
		
	def testModifyParameter(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		
		e.modifyParameters(a={'Id':'new_a'})
		pa = e.m.getListOfParameters()[0]
		self.assertEqual(pa.getId(), 'new_a')
		
		new_kl = e.m.getListOfReactions()[0].getKineticLaw().getFormula()
		self.assertEqual(new_kl, 'new_a * X')
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 3)
		self.assertEqual(e.m.getNumParameters(), 3)
		
	def testModifyReaction(self):
		e = pysbml(sbml_file = DIR + '/test/LV2.xml').sbmlModel()
		
		e.modifyReactions(PreyProduction={'Id':'new_PreyProduction'})
		rea = e.m.getListOfReactions()[0]
		self.assertEqual(rea.getId(), 'new_PreyProduction')
		
		self.assertEqual(e.m.getNumCompartments(), 1)
		self.assertEqual(e.m.getNumSpecies(), 2)
		self.assertEqual(e.m.getNumReactions(), 3)
		self.assertEqual(e.m.getNumParameters(), 3)
		
		
		
		
		
		
		
		
		
		
if __name__ == '__main__':
    unittest.main()
